package com.FH.applyFrom.notic_form;


import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.noticeform_member.NoticeFormMemberResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowPullDTO;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.FileProcess;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wlv on 2016/7/27.
 * <p>
 * 发文管理实现类
 */
@Stateless
public class NoticeFormImpl extends AccessDB implements NoticeFormDAO {


    @Inject
    OperatorLogResource operatorLogResource;

    @Inject
    WorkFlowResource workFlowResource;

    @Inject
    NoticeFormResource noticeFormResource;

    @Inject
    AttachmentResource attachmentResource;

    @Inject
    NoticeFormMemberResource noticeFormMemberResource;

    @Inject
    KamMemberResource kamMemberResource;

    @Inject
    SuggestionResource suggestionResource;

    /**
     * GET
     * 审核、盖章、发送
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNoticeFormInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from notice_form where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("notice_form_id")) {
                sql_w += " AND notice_form_id = '" + json.getString("notice_form_id") + "'";
            }
            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("isverify")) {
                sql_w += " AND isverify = '" + json.getString("isverify") + "'";
            }
//            else {
//                sql_w += " AND (isverify IS NULL OR isverify=0)";
//            }
            if (json.has("notice_form_isgaizhang")) {
                sql_w += " AND notice_form_isgaizhang = '" + json.getString("notice_form_isgaizhang") + "'";
            }
            if (json.has("notice_form_issend")) {
                sql_w += " AND notice_form_issend = '" + json.getString("notice_form_issend") + "'";
            }
            if (json.has("isreadind")) {
                sql_w += " AND isreadind = '" + json.getString("isreadind") + "'";
            }
            if (json.has("iswriteing")) {
                sql_w += " AND iswriteing = '" + json.getString("iswriteing") + "'";
            }
            if (json.has("issending")) {
                sql_w += " AND issending = '" + json.getString("issending") + "'";
            }
            if (json.has("isapprove")) {
                sql_w += " AND isapprove = '" + json.getString("isapprove") + "'";
            }
            if (json.has("isoutside")) {
                sql_w += " AND isoutside = '" + json.getString("isoutside") + "'";
            }


            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeFormShowDTO> result = new ArrayList<NoticeFormShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select notice_form_id,notice_form_type,notice_form_title,attachment_id, " +
                        "member_id,isverify,notice_form_issend,notice_form_isgaizhang,fail_resource,notice_form_content,notice_form_createtime" +
                        ",notice_form_updatetime,id,notice_form_code,notice_form_private,notice_form_copy,notice_form_main,notice_company,notice_fast,isreadind,iswriteing,issending,isapprove,isoutside,isnot from  " +
                        "notice_form where 1=1" + sql_w + " ORDER BY notice_form_createtime DESC" + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    //   NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    nfs.setNotice_form_id(seresult.get(i)[1]);
                    nfs.setNotice_form_type(seresult.get(i)[2]);
                    nfs.setNotice_form_title(seresult.get(i)[3]);
                    nfs.setAttachment_id(seresult.get(i)[4]);
                    nfs.setMember_id(seresult.get(i)[5]);
                    nfs.setIsverify(seresult.get(i)[6]);
                    nfs.setNotice_form_issend(seresult.get(i)[7]);
                    nfs.setNotice_form_isgaizhang(seresult.get(i)[8]);
                    nfs.setFail_resource(seresult.get(i)[9]);
                    nfs.setNotice_form_content(seresult.get(i)[10]);
                    nfs.setNotice_form_createtime(seresult.get(i)[11]);
                    nfs.setNotice_form_updatetime(seresult.get(i)[12]);
                    nfs.setId(seresult.get(i)[13]);
                    nfs.setNotice_form_code(seresult.get(i)[14]);
                    nfs.setNotice_form_private(seresult.get(i)[15]);
                    nfs.setNotice_form_copy(seresult.get(i)[16]);
                    nfs.setNotice_form_main(seresult.get(i)[17]);
                    nfs.setNotice_company(seresult.get(i)[18]);
                    nfs.setNotice_fast(seresult.get(i)[19]);
                    nfs.setIsreadind(seresult.get(i)[20]);
                    nfs.setIswriteing(seresult.get(i)[21]);
                    nfs.setIssending(seresult.get(i)[22]);
                    nfs.setIsapprove(seresult.get(i)[23]);
                    nfs.setIsoutside(seresult.get(i)[24]);
                    nfs.setIsnot(seresult.get(i)[25]);


                    ResultDTO worklist = null, suggestion=null,attachlist = null, fastlist = null, privatelist = null, conpyinfo = null, maininfo = null;

//                    if (json.has("isverify")) {
//                        String state = json.getString("isverify");
//                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + ",work_flow_state:" + state + "}", "1", "50");
//                    } else {
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + "}", "1", "50");
//                    }

                    nfs.setWs(worklist.getValues());


                    attachlist = attachmentResource.getAttachmentInfo("{attachment_id:" + seresult.get(i)[4] + "}", "1", "50");
                    nfs.setAttachlist(attachlist.getValues());

                    //查询发文缓急程度
                    fastlist = noticeFormResource.getNotice_fastInfo("{notice_fast:" + seresult.get(i)[19] + "}", "1", "50");
                    nfs.setFastlist(fastlist.getValues());

                    privatelist = noticeFormResource.getNotice_fastInfo("{notice_form_private:" + seresult.get(i)[15] + "}", "1", "50");
                    nfs.setFastlist(fastlist.getValues());

                    conpyinfo = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[16] + "}", "1", "50");

                    nfs.setCopyinfo(conpyinfo.getValues());

                    maininfo = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[17] + "}", "1", "50");
                    nfs.setMaininfo(maininfo.getValues());

                    if(seresult.get(i)[1] != null && ! seresult.get(i)[1].equals("")){

                        suggestion=suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        nfs.setSuggestionShowDTOs(suggestion.getValues());
                    }
                    result.add(nfs);

                }
            }

            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 查询是或者不是
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getIsnotInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from isnor where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("notice_form_id")) {
                sql_w += " AND notice_form_id = '" + json.getString("notice_form_id") + "'";
            }
            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeFormShowDTO> result = new ArrayList<NoticeFormShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select id,isnot from isnor where 1=1" + sql_w + " ORDER BY id DESC" + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    //   NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    nfs.setId(seresult.get(i)[1]);
                    nfs.setIsnot(seresult.get(i)[2]);

                    result.add(nfs);

                }
            }

            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 添加拟稿的add方法
     * <p>
     * 三个状态字段初始值均为0
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO addNoticeFormInfo(NoticeFormPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        String notice_form_id = dataDTO.getNotice_form_id();
        int resultls = 0;

        String existName = "";
        /**
         * 判断合同申请单中是否已存在此数据
         *
         * 若存在  进行更新
         *
         * 如不存在 执行add方法
         *
         */

        if (notice_form_id != null && !notice_form_id.equals("")) {

            String sql = "select count(1) from notice_form where notice_form_id='" + dataDTO.getNotice_form_id() + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateNoticeFormInfo(dataDTO);
            }
        }


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

//        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-ddHH");
//        String time1=df.format(new Date());

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String id = getImage(dataDTO);

        int result = 0;

        if (id == null) {
            id = "";
        }
        String sql =  "insert into notice_form(notice_form_id,notice_form_type," +
                "notice_form_title,notice_form_content,attachment_id,member_id,notice_form_issend," +
                "notice_form_isgaizhang,notice_form_createtime,notice_form_updatetime,fail_resource,id," +
                "notice_form_code,notice_form_private,notice_form_copy,notice_form_main,notice_company," +
                "notice_fast,isreadind,iswriteing,issending,isapprove,isoutside,isverify,isnot) values" +
                " ('" + uuid + "','" + dataDTO.getNotice_form_type() + "','" + dataDTO.getNotice_form_title() + "','"
                + dataDTO.getNotice_form_content() + "','" + id + "','" + dataDTO.getMember_id() + "','0','0','" + time + "'," +
                "'" + " " + "','" + " " + "','" + dataDTO.getId() + "','" + dataDTO.getNotice_form_code() + "','" + dataDTO.getNotice_form_private() + "'," +
                "'" + dataDTO.getNotice_form_copy() + "','" + dataDTO.getNotice_form_main() + "','" + dataDTO.getNotice_company() + "','" + dataDTO.getNotice_fast() + "','0','0','0','0','" + dataDTO.getIsoutside() + "','0','0')";

        result = this.addReportingTo(sql);

        if (result != 0) {
            resultDTO.setStr(uuid);//
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.add_success);

            //流程上报
            if (dataDTO.getIsoutside().equals("1")) {
                WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
                workFlowPullDTO.setWork_flow_formid(uuid);
                workFlowPullDTO.setWork_flow_type("notice_form");
                workFlowPullDTO.setWork_flow_desc("收文登记请求审批");
                workFlowPullDTO.setWork_flow_date(time);
                workFlowPullDTO.setWork_flow_order("0");
                workFlowPullDTO.setWork_flow_createid(dataDTO.getMember_id());
                workFlowPullDTO.setWork_flow_approval(dataDTO.getReport_people());
                workFlowPullDTO.setWork_flow_approval_organization_id(dataDTO.getWork_flow_approval_organization_id());
                workFlowPullDTO.setFlow_path_id(dataDTO.getFlow_path_id());
                workFlowPullDTO.setWork_flow_suggestion("");
                workFlowResource.addWorkFlowInfo(workFlowPullDTO);

                //日志操作
                OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
                operatorlogPullDTO.setWork_form_id(uuid);
                operatorlogPullDTO.setOperator_log_type("收文申请");
                operatorlogPullDTO.setOperator_log_title(dataDTO.getNotice_form_title());
                operatorlogPullDTO.setOperator_log_content(dataDTO.getNotice_form_content());
                operatorlogPullDTO.setMember_id(dataDTO.getMember_id());
                operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);
            } else {
                WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
                workFlowPullDTO.setWork_flow_formid(uuid);
                workFlowPullDTO.setWork_flow_type("notice_form");
                workFlowPullDTO.setWork_flow_desc("发文拟稿请求审批");
                workFlowPullDTO.setWork_flow_date(time);
                workFlowPullDTO.setWork_flow_order("0");
                workFlowPullDTO.setWork_flow_createid(dataDTO.getMember_id());
                workFlowPullDTO.setWork_flow_approval(dataDTO.getReport_people());
                workFlowPullDTO.setWork_flow_approval_organization_id(dataDTO.getWork_flow_approval_organization_id());
                workFlowPullDTO.setFlow_path_id(dataDTO.getFlow_path_id());
                workFlowPullDTO.setWork_flow_suggestion("");
                workFlowResource.addWorkFlowInfo(workFlowPullDTO);

                //日志操作
                OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
                operatorlogPullDTO.setWork_form_id(uuid);
                operatorlogPullDTO.setOperator_log_type("发文申请");
                operatorlogPullDTO.setOperator_log_title(dataDTO.getNotice_form_title());
                operatorlogPullDTO.setOperator_log_content(dataDTO.getNotice_form_content());
                operatorlogPullDTO.setMember_id(dataDTO.getMember_id());
                operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);
            }


        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;

    }

    /**
     * 修改发文拟稿
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO updateNoticeFormInfo(NoticeFormPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update notice_form set notice_form_updatetime = '" + time + "'";
        if (null != dataDTO.getNotice_form_type() && !"".equals(dataDTO.getNotice_form_type())) {
            QUERY += " ,notice_form_type = '" + dataDTO.getNotice_form_type() + "'";
        }

        if (null != dataDTO.getNotice_form_issend() && !"".equals(dataDTO.getNotice_form_issend())) {
            QUERY += ",notice_form_issend ='" + dataDTO.getNotice_form_issend() + "'";
        }

        if (null != dataDTO.getNotice_form_title() && !"".equals(dataDTO.getNotice_form_title())) {
            QUERY += ",notice_form_title ='" + dataDTO.getNotice_form_title() + "'";
        }

        if (null != dataDTO.getNotice_form_content() && !"".equals(dataDTO.getNotice_form_content())) {
            QUERY += ",notice_form_content ='" + dataDTO.getNotice_form_content() + "'";
        }

        if (null != dataDTO.getIsverify() && !"".equals(dataDTO.getIsverify())) {
            QUERY += ",isverify ='" + dataDTO.getIsverify() + "'";
        }
        if (null != dataDTO.getNotice_form_isgaizhang() && !"".equals(dataDTO.getNotice_form_isgaizhang())) {
            QUERY += ",notice_form_isgaizhang ='" + dataDTO.getNotice_form_isgaizhang() + "'";
        }
//
        if (null != dataDTO.getId() && !"".equals(dataDTO.getId())) {
            QUERY += ",id ='" + dataDTO.getId() + "'";
        }
//
//        if(null!=dataDTO.getNotice_form_send()&&!"".equals(dataDTO.getNotice_form_send())){
//            QUERY += ",notice_form_send ='" + dataDTO.getNotice_form_send() + "'";
//        }
        if (null != dataDTO.getFail_resource() && !"".equals(dataDTO.getFail_resource())) {
            QUERY += ",fail_resource ='" + dataDTO.getFail_resource() + "'";
        }
        if (null != dataDTO.getIsreadind() && !"".equals(dataDTO.getIsreadind())) {
            QUERY += ",isreadind ='" + dataDTO.getIsreadind() + "'";
        }
        if (null != dataDTO.getIswriteing() && !"".equals(dataDTO.getIswriteing())) {
            QUERY += ",iswriteing ='" + dataDTO.getIswriteing() + "'";
        }
        if (null != dataDTO.getIssending() && !"".equals(dataDTO.getIssending())) {
            QUERY += ",issending ='" + dataDTO.getIssending() + "'";
        }
        if (null != dataDTO.getIsapprove() && !"".equals(dataDTO.getIsapprove())) {
            QUERY += ",isapprove ='" + dataDTO.getIsapprove() + "'";
        }
        if (null != dataDTO.getIsoutside() && !"".equals(dataDTO.getIsoutside())) {
            QUERY += ",isoutside ='" + dataDTO.getIsoutside() + "'";
        }
        if (null != dataDTO.getIsnot() && !"".equals(dataDTO.getIsnot())) {
            QUERY += ",isnot ='" + dataDTO.getIsnot() + "'";
        }
        if (null != dataDTO.getAttachment_id() && !"".equals(dataDTO.getAttachment_id()) && null != dataDTO.getFilestream() && !"".equals(dataDTO.getFilestream())) {
            //调用修改文件方法
            updateImage(dataDTO);
        } else if (null != dataDTO.getFilestream() && !"".equals(dataDTO.getFilestream())) {
            String uid = getImage(dataDTO);
            QUERY += ",attachment_id='" + uid + "'";

        }

        QUERY += " where notice_form_id = '" + dataDTO.getNotice_form_id() + "'";


        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(dataDTO.getNotice_form_id());
            workFlowPullDTO.setWork_flow_approval(dataDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(dataDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setIsverify("0");
            workFlowPullDTO.setWork_flow_order(dataDTO.getWork_flow_order());
            workFlowResource.updateWorkFlowInfo(workFlowPullDTO);

//            /**
//             * 调用图片的修改方法
//             *
//             *
//             */
//            updateImage(dataDTO);
            resultDTO.setStr(dataDTO.getNotice_form_id());
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }


    /**
     * 删除发文
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO deleteNoticeFormInfo(NoticeFormPullDTO dataDTO) {

        //删除之前查看该表是否有记录
        String sql = "select count(1) from notice_form where notice_form_id='" + dataDTO.getNotice_form_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";

            QUERY += "delete from notice_form where notice_form_id='" + dataDTO.getNotice_form_id() + "'";

            result = this.addReportingTo(QUERY);
        }
        if (result != 0) {


            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);


            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(dataDTO.getNotice_form_id());
            workFlowResource.deleteWorkFlowInfo(workFlowPullDTO);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);

        }
        return resultDTO;
    }

    /**
     * 根据每个字段去查询信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNoticeotherInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from notice_form where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("notice_form_id")) {
                sql_w += " AND notice_form_id = '" + json.getString("notice_form_id") + "'";
            }
            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("notice_form_isgaizhang")) {
                sql_w += " AND notice_form_isgaizhang = '" + json.getString("notice_form_isgaizhang") + "'";
            }
            if (json.has("notice_form_issend")) {
                sql_w += " AND notice_form_issend = '" + json.getString("notice_form_issend") + "'";
            }
            if (json.has("isreadind")) {
                sql_w += " AND isreadind = '" + json.getString("isreadind") + "'";
            }
            if (json.has("iswriteing")) {
                sql_w += " AND iswriteing = '" + json.getString("iswriteing") + "'";
            }
            if (json.has("issending")) {
                sql_w += " AND issending = '" + json.getString("issending") + "'";
            }
            if (json.has("isapprove")) {
                sql_w += " AND isapprove = '" + json.getString("isapprove") + "'";
            }
            if (json.has("isoutside")) {
                sql_w += " AND isoutside = '" + json.getString("isoutside") + "'";
            }


            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeFormShowDTO> result = new ArrayList<NoticeFormShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select notice_form_id,notice_form_type,notice_form_title,attachment_id, " +
                        "member_id,isverify,notice_form_issend,notice_form_isgaizhang,fail_resource,notice_form_content,notice_form_createtime" +
                        ",notice_form_updatetime,id,notice_form_code,notice_form_private,notice_form_copy,notice_form_main,notice_company,notice_fast,isreadind,iswriteing,issending,isapprove,isoutside from  " +
                        "notice_form where 1=1" + sql_w + " ORDER BY notice_form_createtime DESC" + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    //   NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    nfs.setNotice_form_id(seresult.get(i)[1]);
                    nfs.setNotice_form_type(seresult.get(i)[2]);
                    nfs.setNotice_form_title(seresult.get(i)[3]);
                    nfs.setAttachment_id(seresult.get(i)[4]);
                    nfs.setMember_id(seresult.get(i)[5]);
                    nfs.setIsverify(seresult.get(i)[6]);
                    nfs.setNotice_form_issend(seresult.get(i)[7]);
                    nfs.setNotice_form_isgaizhang(seresult.get(i)[8]);
                    nfs.setFail_resource(seresult.get(i)[9]);
                    nfs.setNotice_form_content(seresult.get(i)[10]);
                    nfs.setNotice_form_createtime(seresult.get(i)[11]);
                    nfs.setNotice_form_updatetime(seresult.get(i)[12]);
                    nfs.setId(seresult.get(i)[13]);
                    nfs.setNotice_form_code(seresult.get(i)[14]);
                    nfs.setNotice_form_private(seresult.get(i)[15]);
                    nfs.setNotice_form_copy(seresult.get(i)[16]);
                    nfs.setNotice_form_main(seresult.get(i)[17]);
                    nfs.setNotice_company(seresult.get(i)[18]);
                    nfs.setNotice_fast(seresult.get(i)[19]);
                    nfs.setIsreadind(seresult.get(i)[20]);
                    nfs.setIswriteing(seresult.get(i)[21]);
                    nfs.setIssending(seresult.get(i)[22]);
                    nfs.setIsapprove(seresult.get(i)[23]);
                    nfs.setIsoutside(seresult.get(i)[24]);


                    ResultDTO attachmentlist = null, worklist = null, conpyinfo = null, maininfo = null,suggestion=null;

//                    if (json.has("isverify")) {
//                        String state = json.getString("isverify");
//                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + ",work_flow_state:" + state + "}", "1", "20");
//                    } else {
//                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + "}", "1", "20");
//                    }
//
//                    nfs.setWs(worklist.getValues());

                    if(seresult.get(i)[1] != null && ! seresult.get(i)[1].equals("")){

                        suggestion=suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        nfs.setSuggestionShowDTOs(suggestion.getValues());
                    }
                    attachmentlist = attachmentResource.getAttachmentInfo("{attachment_id:" + seresult.get(i)[4] + "}", "1", "20");
                    nfs.setAttachlist(attachmentlist.getValues());

                    conpyinfo = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[16] + "}", "1", "50");

                    nfs.setCopyinfo(conpyinfo.getValues());

                    maininfo = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[17] + "}", "1", "50");
                    nfs.setMaininfo(maininfo.getValues());

                    result.add(nfs);

                }
            }

            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }


    /**
     * 查询发文的缓急程度
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNotice_fastInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from noticefast where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("id")) {
                sql_w += " AND id = '" + json.getString("id") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeFormShowDTO> result = new ArrayList<NoticeFormShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select id,noticefast from noticefast where 1=1" + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    //   NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    nfs.setId(seresult.get(i)[1]);
                    nfs.setNoticefast(seresult.get(i)[2]);

                    result.add(nfs);

                }
            }

            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 修改單獨字段，目的：不走修改work_flow表方法
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO updateNoticeFormOtherInfo(NoticeFormPullDTO dataDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update notice_form set notice_form_updatetime = '" + time + "'";
        if (null != dataDTO.getNotice_form_type() && !"".equals(dataDTO.getNotice_form_type())) {
            QUERY += " ,notice_form_type = '" + dataDTO.getNotice_form_type() + "'";
        }

        if (null != dataDTO.getNotice_form_issend() && !"".equals(dataDTO.getNotice_form_issend())) {
            QUERY += ",notice_form_issend ='" + dataDTO.getNotice_form_issend() + "'";
        }

        if (null != dataDTO.getNotice_form_title() && !"".equals(dataDTO.getNotice_form_title())) {
            QUERY += ",notice_form_title ='" + dataDTO.getNotice_form_title() + "'";
        }

        if (null != dataDTO.getNotice_form_content() && !"".equals(dataDTO.getNotice_form_content())) {
            QUERY += ",notice_form_content ='" + dataDTO.getNotice_form_content() + "'";
        }

        if (null != dataDTO.getIsverify() && !"".equals(dataDTO.getIsverify())) {
            QUERY += ",isverify ='" + dataDTO.getIsverify() + "'";
        }
        if (null != dataDTO.getNotice_form_isgaizhang() && !"".equals(dataDTO.getNotice_form_isgaizhang())) {
            QUERY += ",notice_form_isgaizhang ='" + dataDTO.getNotice_form_isgaizhang() + "'";
        }
//
        if (null != dataDTO.getId() && !"".equals(dataDTO.getId())) {
            QUERY += ",id ='" + dataDTO.getId() + "'";
        }
//
//        if(null!=dataDTO.getNotice_form_send()&&!"".equals(dataDTO.getNotice_form_send())){
//            QUERY += ",notice_form_send ='" + dataDTO.getNotice_form_send() + "'";
//        }
        if (null != dataDTO.getFail_resource() && !"".equals(dataDTO.getFail_resource())) {
            QUERY += ",fail_resource ='" + dataDTO.getFail_resource() + "'";
        }
        if (null != dataDTO.getIsreadind() && !"".equals(dataDTO.getIsreadind())) {
            QUERY += ",isreadind ='" + dataDTO.getIsreadind() + "'";
        }
        if (null != dataDTO.getIswriteing() && !"".equals(dataDTO.getIswriteing())) {
            QUERY += ",iswriteing ='" + dataDTO.getIswriteing() + "'";
        }
        if (null != dataDTO.getIssending() && !"".equals(dataDTO.getIssending())) {
            QUERY += ",issending ='" + dataDTO.getIssending() + "'";
        }
        if (null != dataDTO.getIsapprove() && !"".equals(dataDTO.getIsapprove())) {
            QUERY += ",isapprove ='" + dataDTO.getIsapprove() + "'";
        }
        if (null != dataDTO.getIsoutside() && !"".equals(dataDTO.getIsoutside())) {
            QUERY += ",isoutside ='" + dataDTO.getIsoutside() + "'";
        }
        if (null != dataDTO.getIsnot() && !"".equals(dataDTO.getIsnot())) {
            QUERY += ",isnot ='" + dataDTO.getIsnot() + "'";
        }


        QUERY += " where notice_form_id = '" + dataDTO.getNotice_form_id() + "'";


        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 附件表
     *
     * @param image
     * @return
     */

//    public String getImage(String image) {
//        // TODO Auto-generated method stub
//        String enpenform_image = "";
//        //获取配置附件地址
//        String[] image_path = this.getImage_path();
//        String image_pathAddr = image_path[1];//服务器项目目录地址
//        String image_pathSerAddr = image_path[2];//图片访问地址
//        String imgDirct = image_pathAddr + "/notice_form";
//        File f = new File(imgDirct);
//        if (!f.exists()) {
//            f.mkdirs();
//            f.setWritable(true, false);
//        }
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//
//        String imgPath = imgDirct + "/" + uuid + ".jpg";
//
//        //base64字符串保存为图片
//        boolean headImg = FileProcess.GenerateImage(image, imgPath);
//        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
//        int resultls = 0;
//        int mresult = 0;
//
//        String imgurl = image_pathSerAddr + "/notice_form/" + uuid + ".jpg";
//
//        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();
//
//        if (headImg) {
//            //插入头像表
//            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,head_portrait_type) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','"+11+"')";
//        }
//
//        //执行sql
//        resultls = addReportingTo(enpenform_image);
//
//        if (resultls != 0) {
//            return uuid;
//        }
//        return null;
//    }

    /**
     * 添加文件信息
     *
     * @param
     * @return
     */
    public String getImage(NoticeFormPullDTO dataDTO) {

        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/noticeform";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + dataDTO.getFile_type();


        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(dataDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/noticeform/" + dataDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //插入头像表
            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,file_type,file_name) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','" + dataDTO.getFile_type() + "','" + dataDTO.getFile_name() + "')";
            // enpenform_image += "insert into file(file_id,file_addr,file_time,filegui_id,file_name) values ('" + uuid + "','" +imgurl+ "','" +df.format(new Date()) + "','" + dataDTO.getFilegui_id() + "','" + dataDTO.getFiletype() + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);
        }
        if (resultls != 0) {
            return uuid;
        }
        return null;
    }

    /**
     * 修改图片方法
     *
     * @param
     * @return
     */
    public String updateImage(NoticeFormPullDTO dataDTO) {
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/noticeform";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + dataDTO.getFile_type();

        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(dataDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/noticeform/" + dataDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //修改头像表
            enpenform_image += "update attachment set attachment_addr='" + imgurl + "',head_portrait_time = '" + df.format(new Date()) + "',file_type = '" + dataDTO.getFile_type() + "',file_name = '" + dataDTO.getFile_name() + "' where attachment_id='" + dataDTO.getAttachment_id() + "'";
            // enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);
        }


        if (resultls != 0) {
            return uuid;
        }
        return null;
    }
}
